package com.device.android.adapter

import android.hardware.usb.UsbDevice
import android.view.View
import androidx.core.text.HtmlCompat
import androidx.recyclerview.widget.RecyclerView
import com.device.android.R
import com.device.android.databinding.AdapterUsbBinding
import com.loyal.core.adapter.BaseRecycleAdapter

class UsbAdapter : BaseRecycleAdapter<UsbDevice, UsbAdapter.ViewHolder>() {
    override fun createViewHolder(view: View): ViewHolder {
        return ViewHolder(view)
    }

    override fun onViewHolder(holder: ViewHolder, position: Int) {
        val device = getItem(position)
        val devName = "<font color='#8a8a8a'>DevName=</font>" + device.deviceName
        holder.binding.textDevName.text =
            HtmlCompat.fromHtml(devName, HtmlCompat.FROM_HTML_MODE_LEGACY)
        val vendorId = device.vendorId
        val productId = device.productId
        val text =
            "<font color='#8a8a8a'>PID=</font>$productId<font color='#8a8a8a'>，VID=</font>$vendorId"
        holder.binding.textId.text = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY)
        val produceName = "<font color='#8a8a8a'>ProductName=</font>" + device.productName
        val manufacturerName =
            "<font color='#8a8a8a'>ManufacturerName=</font>" + device.manufacturerName
        val serial = "<font color='#8a8a8a'>SerialNumber=</font>" + device.serialNumber
        val desc = String.format("%s; %s; %s", produceName, manufacturerName, serial)
        holder.binding.textDesc.text = HtmlCompat.fromHtml(desc, HtmlCompat.FROM_HTML_MODE_LEGACY)
    }

    override fun adapterLayout(): Int {
        return R.layout.adapter_usb
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val binding by lazy { AdapterUsbBinding.bind(view) }
    }
}